#!/bin/bash

true=0
false=1

g_DN='cn=Manager,dc=black,dc=com'
g_PASS='123456'
g_suffix='dc=black,dc=com'
g_DefGroup="ou=Group,$g_suffix"
g_DefPeople="ou=People,$g_suffix"
g_Domain='black.com'

CheckUserName(){ # function1	CheckUserName <UserName>
	[ "$#" -ne "1" ] && exit 1
	f1_UserNames=(`getent passwd | cut -d ':' -f 1`)
	f1_UserNamesLong=${#f1_UserNames[@]}
	for ((f1_i=0;f1_i<f1_UserNamesLong;f1_i++)); do
		if [ "$1" == "${f1_UserNames[$f1_i]}" ]; then
			return $false
		fi
	done
	return $true
}

CheckGroupName(){ #function2	CheckGroupName <GroupName>
	[ "$#" -ne "1" ] && exit 1
	f2_GroupNames=(`getent group | cut -d ':' -f 1`)
	f2_GroupNamesLong=${#f2_GroupNames[@]}
	for ((f2_i=0;f2_i<f2_GroupNamesLong;f2_i++)); do
		if [ "$1" == "${f2_GroupNames[$f2_i]}" ]; then
			return $false
		fi
	done
	return $true
}

CheckUID(){	# function3	CheckUID <UID>
	[ "$#" -ne "1" ] && exit 1
	f3_UIDs=(`getent passwd | awk -F ':' '{if($3>=1000 && $3<=60000)print $3}'`)
	f3_UIDsLong=${#f3_UIDs[@]}
	for ((f3_i=0;f3_i<f3_UIDsLong;f3_i++)); do
		if [ "$1" == "${f3_UIDs[$f3_i]}" ]; then
			return $false
		fi
	done
	return $true
}

CheckGID(){	# function4	CheckGID <GID>
	[ "$#" -ne "1" ] && exit 1
	f4_GIDs=(`getent group | awk -F ':' '{if($3>=1000 && $3<=60000)print $3}'`)
	f4_GIDsLong=${#f4_GIDs[@]}
	for ((f4_i=0;f4_i<f4_GIDsLong;f4_i++)); do
		if [ "$1" == "${f4_GIDs[$f4_i]}" ]; then
			return $false
		fi
	done
	return $true
}

ChoiseUID(){	# function5	ChoiseUID <UID>
	[ "$#" -ne "0" ] && exit 1
	f5_i=1000
	while ! CheckUID $f5_i; do
		let f5_i++
	done
	echo $f5_i
}

ChoiseGID(){	# function6	ChoiseGID <GID>
	[ "$#" -ne "0" ] && exit 1
	f6_i=1000
	while ! CheckGID $f6_i ;do
		let f6_i++
	done
	echo $f6_i
}

GetGID(){	# function7	GetGID <GroupName>
	[ "$#" -ne "1" ] && exit 1
	echo `getent group | awk -F ':' "/$1/{print \\$3}"`
}

AddGroup(){	# function8	AddGroup <GID> <GroupName>
	[ "$#" -ne "2" ] && exit 1
cat << EOF | ldapadd -x -D $g_DN -w $g_PASS 
dn: cn=$2,$g_DefGroup
objectClass: posixGroup
objectClass: top
cn: $2
userPassword: {crypt}x
gidNumber: $1
EOF
}

AddUser(){	# function9	AddUser <UID> <GID> <UserName>
	[ "$#" -ne "3" ] && exit 1
	until f9_PASS=`slappasswd`; do echo "please retry input passwd"; done
cat << EOF | ldapadd -x -D $g_DN -w $g_PASS
dn: uid=$3,$g_DefPeople
uid: $3
cn: $3
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: $f9_PASS
shadowLastChange: `expr $(date '+%s') / 86400`
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: $1
gidNumber: $2
homeDirectory: /home/$3
gecos: general user
EOF
}

MkdirHome(){	# function10	MkdirHome <UserName> <GroupName>
	[ "$#" -ne "2" ] && exit 1
	if [ ! -d "/home/$1" ]; then
		mkdir /home/$1
		cp -R /etc/skel/. /home/$1
		chown -R $1:$2 /home/$1
        restorecon -R /home/$1
        chmod 700 /home/$1
	fi
}

# Main process
while [ -n "$1" ]; do
	case $1 in
		-G)		# GID
			m_GroupName=$2
			shift 2
		;;
		-h|--help)	# help information
			echo -e "use:\n\t$0 [-G <groupname>] <username>"
			exit 0
		;;
		*)
			m_UserName=$1
			shift 1
		;;
	esac
done
# empty check
if [ ! -n "$m_UserName" ]; then
	echo "error: you don't have input username."
	echo -e "please enter the following for more information:\n\t$0 --help"
	exit 1
fi
[ ! -n "$m_GroupName" ] && m_GroupName=$m_UserName
# Check
if ! CheckUserName $m_UserName; then
	echo "error: user $m_UserName is already exit."
	exit 1
fi
# check Group, if Group is not exit,then create Group
if CheckGroupName $m_GroupName; then
	AddGroup `ChoiseGID` $m_GroupName
fi
# addUser and mkdirhome
AddUser `ChoiseUID` `GetGID $m_GroupName` $m_UserName && MkdirHome $m_UserName $m_GroupName
